package com.ciwong.epaper.modules.reciteWords.util;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.android.volley.TimeoutError;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.cordva.html.PkActivity;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.epaper.util.ListenSpeakUtil;
import com.ciwong.epaper.modules.evaluate.bean.WordDetail;
import com.ciwong.epaper.modules.me.bean.RequirementContent;
import com.ciwong.epaper.modules.me.bean.WorkContents;
import com.ciwong.epaper.modules.reciteWords.bean.JiaoCaiDetailBean;
import com.ciwong.epaper.modules.reciteWords.bean.PlanAndWorListBean;
import com.ciwong.epaper.modules.reciteWords.dao.WordDao;
import com.ciwong.epaper.modules.reciteWords.ui.ChangeWordPlanActivity;
import com.ciwong.epaper.modules.reciteWords.ui.DaKaActivity;
import com.ciwong.epaper.modules.reciteWords.ui.MakeWordPlanActivity;
import com.ciwong.epaper.modules.reciteWords.ui.ReciteWordDetailActivity;
import com.ciwong.epaper.modules.reciteWords.ui.ReciteWordLisActivity;
import com.ciwong.epaper.modules.reciteWords.ui.SelectVocabularyActivity;
import com.ciwong.epaper.modules.reciteWords.ui.WordPlanActivity;
import com.ciwong.epaper.modules.reciteWords.ui.WordStudyActivity;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.IntentValue;
import com.ciwong.epaper.util.download.DownLoadInfo;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.BaseActivityManager;
import com.ciwong.mobilelib.utils.BaseJumpManager;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by keepon on 2018/9/7.
 */
public class WordJumpManager extends BaseJumpManager {



    public static void gotoWordPlan(Activity mActivity) {
        if(!NetworkUtils.isOnline()){
            ToastUtil.INSTANCE.toastCenterNoNetError();
            return;
        }
        //-1表示没计划
        int currentPlanBookId = CWSys.getSharedInt(ListenSpeakUtil.getCurrentPlanBookIdKey(), -1);
        if(currentPlanBookId!=-1){
            jumpToWordPlanActivity(R.string.go_back, IntentValue.JumpWordPlanType.FROM_HOMEWORK,null, mActivity);
        }else{
            judgeJumpWordPlanOrSelectVacabulary(mActivity);
        }

    }

    private static void judgeJumpWordPlanOrSelectVacabulary(final Activity mActivity) {
        //先判断本地是否有列表，有就直接跳转，没有的话，请求网络判断。
        int userId = (int) EApplication.getInstance().getUserInfoBase().getUserId();
        final BaseActivity baseActivity=(BaseActivity) mActivity;
        baseActivity.showCricleProgress();
        WordDao.getInstance().submitWord(userId, new ArrayList<Integer>(),new BaseExtCallBack(mActivity) {
            @Override
            public void failed(int errorCode, Object data) {
                baseActivity.hideCricleProgress();
                super.failed(errorCode,data);
                if(errorCode==-1){
                    jumpToSelectVacabularyActivity(R.string.go_back, mActivity,0, IntentValue.JumpSelectVocabularyType.NORMAL);
                }else{
                    ToastUtil.INSTANCE.toastCenterErrorWithErrorCode(R.string.server_error,errorCode);
                }

            }

            @Override
            public void failed(Object data) {
                baseActivity.hideCricleProgress();
                if (!NetworkUtils.isOnline()) {
                    ToastUtil.INSTANCE.toastCenterNoNetError();
                } else if (data instanceof TimeoutError) {
                    ToastUtil.INSTANCE.toastCenterError("连接超时，请稍后重试");
                }
            }

            @Override
            public void success(Object data) {
                baseActivity. hideCricleProgress();
                //暂无计划
                if(data==null){
                    jumpToSelectVacabularyActivity(R.string.go_back, mActivity,0,IntentValue.JumpSelectVocabularyType.NORMAL);
                }else{
                    PlanAndWorListBean planAndWorListBean = (PlanAndWorListBean) data;
                    CWSys.setSharedInt(ListenSpeakUtil.getCurrentPlanBookIdKey(), planAndWorListBean.getBookId());
                    jumpToWordPlanActivity(R.string.go_back,IntentValue.JumpWordPlanType.FROM_HOMEWORK,planAndWorListBean, mActivity);
                }

            }
        });
    }



    public static void jumpToWordPlanActivity(int goBackId, int jumpFrom, PlanAndWorListBean planAndWorListBean, Activity context) {
        Intent intent = getBaseIntent(goBackId, context, WordPlanActivity.class);
        intent.putExtra(IntentFlag.INTENT_FLAG_JUMP_FROM, jumpFrom);
        intent.putExtra(IntentFlag.INTENT_FLAG_WORD_PLAN_BEAN, planAndWorListBean);
        context.startActivity(intent);

    }

    public static void jumpToChangeWordPlanActivity(int goBackId, String planName, int bookId, int wordsum, int completecount, int dayCount, Activity context) {
        Intent intent = getBaseIntent(goBackId, context, ChangeWordPlanActivity.class);
        intent.putExtra(IntentFlag.INTENT_FLAG_TOTAL_NUM, wordsum);
        intent.putExtra(IntentFlag.INTENT_FLAG_PLAN_NAME, planName);
        intent.putExtra(IntentFlag.INTENT_FLAG_BOOKID, bookId);
        intent.putExtra(IntentFlag.INTENT_FLAG_COMPLETE_COUNT, completecount);
        intent.putExtra(IntentFlag.INTENT_FLAG_SCHEDULE_DAY_COUNT, dayCount);
        context.startActivity(intent);

    }

    public static void jumpToMakeWordPlanActivity(int goBackId, Activity context, JiaoCaiDetailBean jiaoCaiDetailBean,int completecount,boolean isLearnAgain) {
        Intent intent = getBaseIntent(goBackId, context, MakeWordPlanActivity.class);
        intent.putExtra(IntentFlag.INTENT_FLAG_JIAOCAI_DETAIL, jiaoCaiDetailBean);
        intent.putExtra(IntentFlag.INTENT_FLAG_COMPLETE_COUNT, completecount);
        intent.putExtra(IntentFlag.INTENT_FLAG_WORD_LEARN_AGAIN, isLearnAgain);
        context.startActivity(intent);

    }

    public static void jumpToSelectVacabularyActivity(int goBackId, Activity context, int bookId, int jumpFrom) {
        Intent intent = getBaseIntent(goBackId, context, SelectVocabularyActivity.class);
        intent.putExtra(IntentFlag.INTENT_FLAG_BOOKID, bookId);
        intent.putExtra(IntentFlag.INTENT_FLAG_JUMP_FROM, jumpFrom);
        context.startActivity(intent);
    }

    public static void jumpToDaKaActivity(int goBackId, Activity context, ArrayList<Integer> wordIndexArray) {
        Intent intent = getBaseIntent(goBackId, context, DaKaActivity.class);
        intent.putExtra(IntentFlag.INTENT_FLAG_WORD_INDEX_ARRAY, wordIndexArray);
        context.startActivity(intent);

    }

    public static void jumpToWordStudyActivity(int goBackId, CopyOnWriteArrayList<WordDetail> todayWords,
                                               CopyOnWriteArrayList<WordDetail> reviewWords, Activity context, int requestCode) {
        Intent intent = getBaseIntent(goBackId, context, WordStudyActivity.class);
        intent.putExtra(IntentFlag.INTENT_FLAG_NEW_WORD, todayWords);
        intent.putExtra(IntentFlag.INTENT_FLAG_REVIEW_WORD, reviewWords);
        intent.putExtra(IntentFlag.INTENT_FLAG_REVIEW_WORD, reviewWords);
        intent.putExtra(IntentFlag.INTENT_FLAG_REQUESTCODE, requestCode);
        context.startActivityForResult(intent, requestCode);

    }

    public static void jumpToWordPKActivity(int goBackId, Activity context) {
        Intent intent = getBaseIntent(goBackId, context, PkActivity.class);
        context.startActivity(intent);

    }

    public static void jumpToReciteWordLisActivity(int goBackId, Activity context) {
        Intent intent = getBaseIntent(goBackId, context, ReciteWordLisActivity.class);
        context.startActivity(intent);

    }

    public static void jumpToReciteWordDetailActivity(int goBackId, Activity context, int flagFrom, String bookId, String did) {
        int JUMP_CODE_RECITY_WORD = 1;
        Intent intent = getBaseIntent(goBackId, context, ReciteWordDetailActivity.class);
        intent.putExtra(IntentFlag.INTENT_FLAG_FORM, flagFrom);
        if (TextUtils.isEmpty(bookId))
            intent.putExtra(IntentFlag.INTENT_FLAG_RECITE_VERSION, "");
        else
            intent.putExtra(IntentFlag.INTENT_FLAG_RECITE_VERSION, bookId);

        intent.putExtra(IntentFlag.INTENT_FLAG_RECITE_INTERATION_ID, did);
        context.startActivityForResult(intent, JUMP_CODE_RECITY_WORD);

    }



}
